package User;

import StateInformation.GameState;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class FileUserManager implements UserManager {

    private String userListPath = "C:/Users/Jonatan/Desktop/userList.txt";

    @Override
    public boolean register(String username, String password, String name, String surname, String email) {
        try {
            User user = new User(username, surname, name, password, email);

            File file = new File(userListPath);
            if (!file.exists()) {
                file.createNewFile();
            }

            FileInputStream fileInputStream = new FileInputStream(file);
            ArrayList<User> userList;
            if (fileInputStream.read() != -1) {
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                objectInputStream.close();
                userList = (ArrayList<User>) objectInputStream.readObject();
            } else {
                userList = new ArrayList<User>();
            }


            for (User u : userList) {
                if (u.getUsername().equals(user.getUsername())) {
                    return false;
                }
            }

            userList.add(user);

            file.delete();
            file.createNewFile();

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(userList);
            objectOutputStream.flush();
            objectOutputStream.close();
            return true;
        } catch (Exception e) {
            try {
                File file = new File("C:/FileUserManager_register.txt");
                file.createNewFile();
            } catch (Exception ex) {
            }
        }
        return false;
    }

    @Override
    public boolean logIn(String username, String password) {
        try {
            File file = new File(userListPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

            ArrayList<User> userList = (ArrayList<User>) objectInputStream.readObject();
            objectInputStream.close();

            for (User u : userList) {
                if (u.getUsername().equals(username)) {
                    return true;
                }
            }
        } catch (Exception e) {
        }
        return false;
    }

    @Override
    public void setGameState(String username, GameState gameState) {
        try {
            File file = new File(userListPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

            ArrayList<User> userList = (ArrayList<User>) objectInputStream.readObject();
            objectInputStream.close();

            for (User u : userList) {
                if (u.getUsername().equals(username)) {
                    u.setGameState(gameState);
                }
            }

            file.delete();
            file.createNewFile();

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
            objectOutputStream.writeObject(userList);
            objectOutputStream.flush();
            objectOutputStream.close();
        } catch (Exception e) {
            try {
                File file = new File("C:/FileUserManager_setPhase.txt");
                file.createNewFile();
            } catch (Exception ex) {
            }
        }
    }

    @Override
    public GameState getGameState(String username) {
        try {
            File file = new File(userListPath);
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);

            ArrayList<User> userList = (ArrayList<User>) objectInputStream.readObject();
            objectInputStream.close();

            for (User u : userList) {
                if (u.getUsername().equals(username)) {
                    return u.getGameState();
                }
            }
        } catch (Exception e) {
            try {
                File file = new File("C:/FileUserManager_getPhase.txt");
                file.createNewFile();
            } catch (Exception ex) {
            }
        }
        return null;
    }
}
